package com.vstudy.zx.service.impl;

import com.vstudy.zx.dao.*;
import com.vstudy.zx.entity.*;
import com.vstudy.zx.service.ArticleService;
import com.vstudy.zx.utils.IOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.UUID;

@Service
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleDao articleDao;
    @Autowired
    private GradeDao gradeDao;
    @Autowired
    private SubjectDao subjectDao;
    @Autowired
    private OutlineDao outlineDao;
    @Autowired
    private ArticleZanDao articleZanDao;
    @Autowired
    private CommentsDao commentsDao;

    @Override
    @Transactional(readOnly = true)
    public List<Grade> getGrades() {
        return gradeDao.select();
    }

    @Override
    @Transactional(readOnly = true)
    public List<Subject> getSubjects() {
        return subjectDao.select();
    }

    @Override
    public List<Outline> getOutline(Long grade_id, Long subject_id) {
        return outlineDao.selectBy(grade_id, subject_id);
    }

    @Override
    public boolean saveArticle(String html, String title, Long te_id, Long outline_id, HttpServletRequest request) {
        String path = "/upload/article/";
        String username = UUID.randomUUID().toString();
        String realPath = request.getServletContext().getRealPath(path);
        File dir = new File(realPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        Article article = new Article();
        article.setArticle_release_time(new Date());  // 发布时间
        article.setAuthor(new Teacher() {{
            setTe_id(te_id);
        }});  // 作者
        article.setArticle_title(title);  // 标题
        article.setOutline(new Outline() {{
            setO_id(outline_id);
        }});   // 知识点大纲
        try {
            IOUtil.writeArticle(realPath + username, "utf-8", html);  // 内容
            article.setArticle_content(path + username); //考虑项目移植，使用虚拟路径
            int insert = articleDao.insert(article);
            if (insert < 1) {
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return true;
    }

    @Override
    public Article getArticleById(Long id, HttpServletRequest request) {
        Article article = articleDao.selectById(id);
        String realPath = request.getServletContext().getRealPath(article.getArticle_content());
        try {
            String html = IOUtil.readArticle(realPath, "utf-8");
            article.setArticle_content(html);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return article;
    }

    @Override
    public ArticleZan getZan(Long article_id, int role, Long role_id) {
        return articleZanDao.selectBy(article_id, role, role_id);
    }

    @Override
    public boolean zan(Long article_id, int role, Long role_id) {
        ArticleZan zan = articleZanDao.selectBy(article_id, role, role_id);
        if (zan == null) {  // 点赞
            int rs = articleZanDao.add(article_id, role, role_id);
            if (rs > 0) {
                int update = articleDao.addZan(article_id);
                return update > 0;
            } else {
                return false;
            }

        } else {
            if (zan.getZan_status()) {
                articleDao.reduceZan(article_id);
            } else {
                articleDao.addZan(article_id);
            }
            zan.setZan_status(!zan.getZan_status());

            int rs = articleZanDao.update(zan);
            return rs > 0;
        }
    }

    @Override
    @Transactional(readOnly = true)
    public List<Article> getArticles(Set<Long> ids, HttpServletRequest request) {
        List<Article> articles = articleDao.selectByIds(ids);
        for (Article a :
                articles) {
            String realPath = request.getServletContext().getRealPath(a.getArticle_content());
            try {
                String html = IOUtil.readArticle(realPath, "utf-8");
                a.setArticle_content(html);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return articles;
    }

    @Override
    public int addReadCount(Long article_id) {
        return articleDao.addReadCount(article_id);
    }

    @Override
    public StarComments getStarComments(Long article_id, int role, Long te_id) {
        StarComments comments = new StarComments();
        Article article = new Article();
        article.setArticle_id(article_id);
        comments.setArticle(article);
        comments.setSc_role(role);
        comments.setRole_id(te_id);
        return commentsDao.select(comments);
    }
}
